package stark.app.function.ocr;

import android.net.Uri;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.List;

import stark.app.R;
import stark.app.databinding.ActivityOcrDemoBinding;
import stark.app.function.imgocr.ImgOcrDemoActivity;
import stark.common.apis.ApiManager;
import stark.common.apis.base.ImgAnimalRet;
import stark.common.apis.base.ImgCarRet;
import stark.common.apis.base.ImgPlantRet;
import stark.common.apis.base.OcrBankCardRet;
import stark.common.apis.base.OcrBusinessLicenseRet;
import stark.common.apis.base.OcrIdCardRet;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2023/5/26
 * Email：1027154338@qq.com
 * Desc:
 */
public class OcrDemoActivity extends BaseNoModelActivity<ActivityOcrDemoBinding> {

    private static final int TYPE_PLANT = 1;
    private static final int TYPE_ANIMAL = 2;
    private static final int TYPE_WORD = 3;
    private static final int TYPE_ID_CARD = 4;
    private static final int TYPE_BANK_CARD = 5;
    private static final int TYPE_BUSINESS_LICENSE = 6;
    private static final int TYPE_CAR = 7;

    @Override
    protected int onCreate() {
        return R.layout.activity_ocr_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnPlant.setOnClickListener(v -> {
            selPicToOcr(TYPE_PLANT);
        });
        mDataBinding.btnAnimal.setOnClickListener(v -> {
            selPicToOcr(TYPE_ANIMAL);
        });
        mDataBinding.btnWord.setOnClickListener(v -> {
            startActivity(ImgOcrDemoActivity.class);
        });
        mDataBinding.btnIdCard.setOnClickListener(v -> {
            selPicToOcr(TYPE_ID_CARD);
        });
        mDataBinding.btnBankCard.setOnClickListener(v -> {
            selPicToOcr(TYPE_BANK_CARD);
        });
        mDataBinding.btnBusinessLicense.setOnClickListener(v -> {
            selPicToOcr(TYPE_BUSINESS_LICENSE);
        });
        mDataBinding.btnCar.setOnClickListener(v -> {
            selPicToOcr(TYPE_CAR);
        });

    }

    @Override
    protected void initData() {

    }

    private void selPicToOcr(int type) {
        PhotoSelectorBuilder
                .builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(1)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        if (data == null || data.size() == 0) {
                            return;
                        }
                        String uriStr = data.get(0).getUri();
                        Uri uri = Uri.parse(uriStr);
                        switch (type) {
                            case TYPE_PLANT:
                                identifyPlant(uri);
                                break;
                            case TYPE_ANIMAL:
                                identifyAnimal(uri);
                                break;
                            case TYPE_ID_CARD:
                                identifyIdCard(uri);
                                break;
                            case TYPE_BANK_CARD:
                                identifyBankCard(uri);
                                break;
                            case TYPE_BUSINESS_LICENSE:
                                identifyBusinessLicense(uri);
                                break;
                            case TYPE_CAR:
                                identifyCar(uri);
                                break;
                        }
                    }
                });
    }

    private void identifyPlant(Uri uri) {
        showDialog("识别中...");
        ApiManager.imgApi().identifyPlant(this, uri, new IReqRetCallback<List<ImgPlantRet>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<ImgPlantRet> result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (result != null) {
                    mDataBinding.tvRet.setText(GsonUtils.toJson(result));
                }
            }
        });
    }

    private void identifyAnimal(Uri uri) {
        showDialog("识别中...");
        ApiManager.imgApi().identifyAnimal(this, uri, new IReqRetCallback<List<ImgAnimalRet>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<ImgAnimalRet> result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (result != null) {
                    mDataBinding.tvRet.setText(GsonUtils.toJson(result));
                }
            }
        });
    }

    private void identifyIdCard(Uri uri) {
        showDialog("识别中...");
        ApiManager.ocrApi().identifyIdCard(this, uri, new IReqRetCallback<OcrIdCardRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable OcrIdCardRet result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (result != null) {
                    mDataBinding.tvRet.setText(GsonUtils.toJson(result));
                }
            }
        });
    }

    private void identifyBankCard(Uri uri) {
        showDialog("识别中...");
        ApiManager.ocrApi().identifyBankCard(this, uri, new IReqRetCallback<OcrBankCardRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable OcrBankCardRet result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (result != null) {
                    mDataBinding.tvRet.setText(GsonUtils.toJson(result));
                }
            }
        });
    }

    private void identifyBusinessLicense(Uri uri) {
        showDialog("识别中...");
        ApiManager.ocrApi().identifyBusinessLicense(this, uri, new IReqRetCallback<OcrBusinessLicenseRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable OcrBusinessLicenseRet result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (result != null) {
                    mDataBinding.tvRet.setText(GsonUtils.toJson(result));
                }
            }
        });
    }

    private void identifyCar(Uri uri) {
        showDialog("识别中...");
        ApiManager.imgApi().identifyCar(this, uri, new IReqRetCallback<List<ImgCarRet>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<ImgCarRet> result) {
                dismissDialog();
                ToastUtils.showShort(msg);
                if (result != null) {
                    mDataBinding.tvRet.setText(GsonUtils.toJson(result));
                }
            }
        });
    }
}
